#include <dbg.h>

#include <cassert>

using namespace std;

class Solution {
public:
    int longestSemiRepetitiveSubstring(string s) {
        int res = 1;
        int cnt = 0;
        int left = 0;
        for (int right = 1; right < s.size(); ++right) {
            if (s[right] == s[right - 1]) {
                ++cnt;
            }
            if (cnt > 1) {
                ++left;
                while (s[left] != s[left - 1]) {
                    ++left;
                }
                --cnt;
            }
            res = max(res, right - left + 1);
        }
        return res;
    }
};

int main() {
    Solution s;

    assert(s.longestSemiRepetitiveSubstring("52233") == 4);
    assert(s.longestSemiRepetitiveSubstring("5494") == 4);
    assert(s.longestSemiRepetitiveSubstring("1111111") == 2);
    assert(s.longestSemiRepetitiveSubstring("0") == 1);

    return 0;
}